#ifndef __NETBASE_H__
#define __NETBASE_H__

#pragma once

//#define __USE_WIN32THREAD__
#define __USE_HAWKTHREAD__

#include "nl.h"
#include "hawkthreads.h"
#include "SyncLib/SyncManager.h"


#define MAX_CLIENTS 2
#define SERVER_ADDR "192.168.0.100:25000"
#define SERVER_PORT 25000
#define FRAME_LATENCY 2
#define OSC_PORT 7000

#define CMD_NONE 9
#define CMD_CAMSET 10
#define CMD_START 11
#define CMD_STOP 12
#define CMD_STARTANIM 13
#define CMD_EVENT 14
#define CMD_NEXTSCENE 15
#define CMD_PREVSCENE 16
#define CMD_SETSCENE 17
#define CMD_SETREADY 18

struct SShared
{
	enum EStatus
	{
		e_None,
		e_Status_Received,
		e_Status_Sent,
		e_Status_Execute,
		e_Max,
	};

	SShared(int netId) : m_NetId(netId), m_Sent(e_Status_Received), m_AnimationsFrame(0), m_MovieFrame(CSyncManager::getSingleton().m_MovieFrame), m_Time(0), m_IsReady(false) {};
	struct SCamera
	{
		Vector3 m_Position;
		Quaternion m_Orientation;
	};

	long m_NetId;
	char m_Command;
	unsigned long m_FrameNumber;
	EStatus m_Sent;
	int m_MovieFrame;
	Real m_Time;
	bool m_IsMovie;
	bool m_IsReady;

	union
	{
		struct
		{
			Real m_AnimationsFrame;
			SCamera m_Camera;
			int m_SceneId;
			//union
			//{
				Real m_animPacket[MAX_ANIM];
				int m_moviesFrame[MAX_MOVIE];
			//};
		};
		SOpenMoveDisplayPacket m_OSCPacket;
	};
};

class COgreEngine;

class CNetBase : public FrameListener
{
public:
	CNetBase(COgreEngine *pEngine);
	virtual ~CNetBase(void);

	virtual void Create(void);
	virtual void Destroy(void);

	virtual bool frameStarted(const FrameEvent& evt);

public:
	virtual void WaitUntilReady(void);
	virtual bool SendPacket(NLsocket Peer, void *pBuffer, unsigned int Size);

public:
	NLsocket m_Socket;
	bool m_IsReady;
	bool m_Shutdown;

	COgreEngine * m_pEngine;

	typedef std::vector<SShared> TCommands;
	TCommands m_CommandQueue;
	int m_NetID;

#ifdef __USE_HAWKTHREAD__
	HTmutex m_QueueReadMutex;
	HThreadID m_MainThread;
#endif
#ifdef __USE_WIN32THREAD__
	static CRITICAL_SECTION m_QueueReadSection;
#endif
};

#endif //__NETBASE_H__
